"""
Case Type   : 功能测试
Case Name   : 集群执行扩缩容
Create At   : 2025/03/25
Owner       : @chen
Description :
    1.缩容备2
    2.查看共享文件
    3.扩容备机2
    4.查看共享文件
    5.每个节点查看集群控制文件：
pg_controldata +data
    6.恢复环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.正常
    6.成功
History     :
"""

import unittest
import os
import time
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.ComThread import ComThread
from yat.test import Node
from yat.test import macro
logger = Logger()

class Test_Xlog_Syncretism_0013(unittest.TestCase):
    def setUp(self):
        logger.info(f'--------{os.path.basename(__file__)} start--------')
        self.pri_node = Node('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.sta1_node = Node('Standby1DbUser')
        self.sta2_node = Node('Standby2DbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta2_root_node = Node('Standby2Root')
        self.constant = Constant()
        self.com = Common()
        self.db_host = self.sta2_node.db_host
        self.s_user = self.pri_node.ssh_user
        self.pri_tpcc_log = 'pri_tpcc_result.log'
        self.sta_tpcc_log = 'sta_tpcc_result.log'
    
    def test_shared_storage(self):
        text = f'''-----step1: 执行缩容备二 expect: 工具执行成功-----'''
        logger.info(text)
        cmd = f'''source {macro.DB_ENV_PATH};
        expect <<EOF
        set timeout 600
        spawn gs_dropnode -U {self.s_user} -G {self.s_user} \
            -h {self.db_host}
        expect "*drop the target node (yes/no)?*"
        send "yes\\n"
        expect eof\n''' + '''EOF'''
        logger.info(cmd)
        res = self.pri_node.sh(cmd).result()
        logger.info(res)
        self.assertIn('Success to drop the target nodes', res)
        cmd = f'source {macro.DB_ENV_PATH}; gs_uninstall --delete-data'
        logger.info(cmd)
        res = self.sta2_node.sh(cmd).result()
        logger.info(res)
        self.assertIn('Uninstallation succeeded', res)
        
        text = '-----step2:查看共享文件 expect:成功-----'
        logger.info(text)
        logger.info('-----xlog共享文件校验-----')
        cmd_data = f'source {macro.DB_ENV_PATH};dsscmd ls -p +data'
        cmd_log = f'source {macro.DB_ENV_PATH};dsscmd ls -p +log'
        logger.info('-----主节点校验-----')
        logger.info(cmd_data)
        res = self.pri_node.sh(cmd_data).result()
        logger.info(res)
        logger.info(cmd_log)
        res_log = self.pri_node.sh(cmd_log).result()
        logger.info(res_log)
        self.assertTrue(1 == res.count('pg_xlog'), '主节点校验失败')
        logger.info('-----pg_xlog共享文件校验-----')
        self.assertTrue(1 == res.count('pg_xlog'), '主节点校验失败')
        self.assertTrue(1 == res_log.count('pg_xlog'), '备1节点校验失败')
        logger.info('-----pg_doublewrite共享文件校验-----')
        self.assertTrue(1 == res.count('pg_doublewrite'), '主节点校验失败')
        logger.info('-----pg_control共享文件校验-----')
        self.assertTrue(1 == res.count('pg_control '), '主节点校验失败')
        logger.info('-----备1节点校验-----')
        logger.info(cmd_data)
        res = self.sta1_node.sh(cmd_data).result()
        logger.info(res)
        logger.info(cmd_log)
        res_log = self.sta1_node.sh(cmd_log).result()
        logger.info(res_log)
        logger.info('-----pg_xlog共享文件校验-----')
        self.assertTrue(1 == res.count('pg_xlog'), '备1节点校验失败')
        self.assertTrue(1 == res_log.count('pg_xlog'), '备1节点校验失败')
        logger.info('-----pg_doublewrite共享文件校验-----')
        self.assertTrue(1 == res.count('pg_doublewrite'), '备1节点校验失败')
        logger.info('-----pg_control共享文件校验-----')
        self.assertTrue(1 == res.count('pg_control '), '备1节点校验失败')

        text = '-----step3:扩容备二 expect:成功-----'
        xml_path = macro.DB_XML_PATH
        cmd = f'''gs_expansion -U {self.sta2_node.ssh_user} -G \
            {self.sta2_node.ssh_user} -X {xml_path} -h {self.sta2_node.ssh_host}'''
        execute_cmd = f'''source {macro.DB_ENV_PATH};
        expect <<EOF
        set timeout 300
        spawn {cmd}
        expect "*assword*:"
        send "{self.sta2_root_node.ssh_password}\\n"
        expect "*assword*:"
        send "{self.sta2_root_node.ssh_password}\\n"
        expect "*lease repeat for database*:"
        send "{macro.GAUSSDB_INIT_USER_PASSWD}\\n"
        expect eof\n''' + '''EOF'''
        logger.info(execute_cmd)
        res = self.pri_root.sh(execute_cmd).result()
        logger.info(res)
        self.assertIn(f'{self.sta2_node.ssh_host}:\tSuccess', res)

        text = '-----step4:查看共享文件 expect:成功-----'
        logger.info(text)
        logger.info('-----xlog共享文件校验-----')
        cmd_data = f'source {macro.DB_ENV_PATH};dsscmd ls -p +data'
        cmd_log = f'source {macro.DB_ENV_PATH};dsscmd ls -p +log'
        logger.info('-----主节点校验-----')
        logger.info(cmd_data)
        res = self.pri_node.sh(cmd_data).result()
        logger.info(res)
        logger.info(cmd_log)
        res_log = self.pri_node.sh(cmd_log).result()
        logger.info(res_log)
        self.assertTrue(1 == res.count('pg_xlog'), '主节点校验失败')
        logger.info('-----pg_xlog共享文件校验-----')
        self.assertTrue(1 == res.count('pg_xlog'), '主节点校验失败')
        self.assertTrue(1 == res_log.count('pg_xlog'), '备1节点校验失败')
        logger.info('-----pg_doublewrite共享文件校验-----')
        self.assertTrue(1 == res.count('pg_doublewrite'), '主节点校验失败')
        logger.info('-----pg_control共享文件校验-----')
        self.assertTrue(1 == res.count('pg_control '), '主节点校验失败')
        logger.info('-----备1节点校验-----')
        logger.info(cmd_data)
        res = self.sta1_node.sh(cmd_data).result()
        logger.info(res)
        logger.info(cmd_log)
        res_log = self.sta1_node.sh(cmd_log).result()
        logger.info(res_log)
        logger.info('-----pg_xlog共享文件校验-----')
        self.assertTrue(1 == res.count('pg_xlog'), '备1节点校验失败')
        self.assertTrue(1 == res_log.count('pg_xlog'), '备1节点校验失败')
        logger.info('-----pg_doublewrite共享文件校验-----')
        self.assertTrue(1 == res.count('pg_doublewrite'), '备1节点校验失败')
        logger.info('-----pg_control共享文件校验-----')
        self.assertTrue(1 == res.count('pg_control '), '备1节点校验失败')
        logger.info('-----备2节点校验-----')
        logger.info(cmd_data)
        res = self.sta1_node.sh(cmd_data).result()
        logger.info(res)
        logger.info(cmd_log)
        res_log = self.sta1_node.sh(cmd_log).result()
        logger.info(res_log)
        logger.info('-----pg_xlog共享文件校验-----')
        self.assertTrue(1 == res.count('pg_xlog'), '备2节点校验失败')
        self.assertTrue(1 == res_log.count('pg_xlog'), '备2节点校验失败')
        logger.info('-----pg_doublewrite共享文件校验-----')
        self.assertTrue(1 == res.count('pg_doublewrite'), '备2节点校验失败')
        logger.info('-----pg_control共享文件校验-----')
        self.assertTrue(1 == res.count('pg_control '), '备2节点校验失败')

        text = '-----step5:每个节点查看集群控制文件 expect:in production-----'
        logger.info(text)
        cmd = f'source {macro.DB_ENV_PATH}; pg_controldata +data'
        logger.info('-----主节点校验-----')
        logger.info(cmd)
        res = self.pri_node.sh(cmd).result()
        logger.info(res)
        self.assertIn('Database cluster state:               in production', 
            res, '主节点校验失败')
        logger.info('-----备1节点校验-----')
        logger.info(cmd)
        res = self.sta1_node.sh(cmd).result()
        logger.info(res)
        self.assertIn('Database cluster state:               in production', 
            res, '备2节点校验失败')
        logger.info('-----备2节点校验-----')
        logger.info(cmd)
        res = self.sta2_node.sh(cmd).result()
        logger.info(res)
        self.assertIn('Database cluster state:               in production', 
            res, '备2节点校验失败')
    
    def tearDown(self):
        text = '-----step6:恢复环境 expect:成功-----'
        logger.info(text)
        logger.info('------检查数据库状态是否正常------')
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        logger.info(status)
        self.assertIn("cluster_state   : Normal", status)
        logger.info(f'-----{os.path.basename(__file__)} end-----')